﻿//EXAMPLE USAGE:
//	var myWriter:swfWriter = swfWriter.getInstance();
//	myWriter.createFile(320,240, 30, 120);
//	myWriter.saveFrame( myBitmapData1 );
//	myWriter.saveFrame( myBitmapData2 );
//	myWriter.saveFrame( myBitmapData3 ); // etc.
//	myWriter.finish();
package com.jacky.enconder
    import org.libspark.swfassist.swf.io.*;
    import org.libspark.swfassist.swf.tags.*;
    import org.libspark.swfassist.swf.structures.*;
    import org.libspark.swfassist.io.*;
    import com.adobe.images.JPGEncoder;
    import flash.utils.ByteArray;
    import flash.display.BitmapData; 
	import flash.net.FileReference;

    public class swfWriter {
            private var _quality:uint;
            private var _width:uint;
            private var _height:uint;
            private var _fps:uint;
            //
            private var _jpgEnc:JPGEncoder;
            //
            private var _swf:SWF;
            //
            private var _frame:uint;
            //
            private var _tagSCR:StyleChangeRecord;
            private var _tagSER1:StraightEdgeRecord;
            private var _tagSER2:StraightEdgeRecord;
            private var _tagSER3:StraightEdgeRecord;
            private var _tagSER4:StraightEdgeRecord;
            private var _tagSF:ShowFrame; 
			static private var _instance:swfWriter;
			public static function getInstance():swfWriter 
			{
				if(swfWriter._instance == null) 
					swfWriter._instance = new swfWriter();
				return swfWriter._instance;
			}

			public function swfWriter()
			{
			}

			public function createFile(width:uint=320,height:uint=240,fps:uint=12,quality:uint=50) {
                    _width = width;
                    _height = height;
                    _quality = quality;
                    _fps = fps; 

                    _tagSCR = new StyleChangeRecord();
                    _tagSCR.stateFillStyle1=true;
                    _tagSCR.stateMoveTo = true;
                    _tagSCR.moveDeltaX = _width;
                    _tagSCR.moveDeltaY = _height;
                    _tagSCR.fillStyle1=1;
                    _tagSCR.fillStyle0=0; 

                    _tagSER1= new StraightEdgeRecord();
                    _tagSER1.generalLine=false;
                    _tagSER1.verticalLine=false;
                    _tagSER1.deltaX=-_width;
                    _tagSER1.deltaY=0; 

                    _tagSER2= new StraightEdgeRecord();
                    _tagSER2.generalLine=false;
                    _tagSER2.verticalLine=true;
                    _tagSER2.deltaX=0;
                    _tagSER2.deltaY=-_height; 

                    _tagSER3= new StraightEdgeRecord();
                    _tagSER3.generalLine=false;
                    _tagSER3.verticalLine=false;
                    _tagSER3.deltaX=_width;
                    _tagSER3.deltaY=0; 

                    _tagSER4= new StraightEdgeRecord();
                    _tagSER4.generalLine=false;
                    _tagSER4.verticalLine=true;
                    _tagSER4.deltaX=0;
                    _tagSER4.deltaY=_height; 

                    _tagSF = new ShowFrame();
                    _jpgEnc = new JPGEncoder(_quality);
                    start();
            }
            public function get frame():uint{
                    return _frame
            }
            public function start() {
                    _swf = new SWF();
                    _swf.header.version = 9;
                    _swf.header.frameSize.xMax = _width;
                    _swf.header.frameSize.yMax = _height;
                    _swf.header.isCompressed = true;
                    _swf.header.frameRate = _fps;
                    _swf.tags.addTag(new SetBackgroundColor());
                    _frame = 1;
            }
            public function saveFrame(bmpData:BitmapData) {
                    //define bits jpeg2
                    var tagDBJ = new DefineBitsJPEG2();
                    tagDBJ.characterId = (_frame-1)*2+1;
                    tagDBJ.jpegData = _jpgEnc.encode(bmpData);
                    //define shape
                    var tagDS = new DefineShape();
                    tagDS.shapeId = (_frame-1)*2+2;
                    tagDS.shapeBounds.xMin = 0;
                    tagDS.shapeBounds.xMax = _width;
                    tagDS.shapeBounds.yMin = 0;
                    tagDS.shapeBounds.yMax = _height;
                    //define fill style
                    var tagFS = new FillStyle();
                    tagFS.color.fromUint(0xff000000);
                    tagFS.fillStyleType = 67;
                    tagFS.bitmapId = (_frame-1)*2+1;
                    tagFS.bitmapMatrix.hasScale = true;
                    tagFS.bitmapMatrix.scaleX = 20;
                    tagFS.bitmapMatrix.scaleY = 20;
                    //
                    tagDS.shapes.shapeRecords = [_tagSCR,_tagSER1,_tagSER2,_tagSER3,_tagSER4];
                    tagDS.shapes.fillStyles.fillStyles = [tagFS];
                    //
                    var tagPO = new PlaceObject2();
                    tagPO.hasCharacter = true;
                    tagPO.hasMatrix = true;
                    tagPO.depth=_frame;
                    tagPO.characterId = (_frame-1)*2+2;
                    //
                    _swf.tags.addTag(tagDBJ);
                    _swf.tags.addTag(tagDS);
                    _swf.tags.addTag(tagPO);
                    _swf.tags.addTag(_tagSF);    
                    //
                    _frame++;
            }
            public function finish() {
                    var data:ByteArray = new ByteArray();
                    var swfwriter:SWFWriter = new SWFWriter();
                    var out = new ByteArrayOutputStream(data);
                    var context = new WritingContext();
                    swfwriter.writeSWF(out, context, _swf);
					var fr:FileReference = new FileReference();
					fr.save(data,"1.swf")
                    //return data
            }
    }
}